package org.hw.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hw.entity.User;
import org.hw.mapper.UserMapper;
import org.hw.service.UserService;
import org.hw.util.PageObject;
import org.hw.util.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Validated
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserMapper userMapper;

    @Autowired
    UserService userService;

    /**
     * 查询单个
     *
     * @return
     */
    @GetMapping("/detail")
    @ApiOperation("按id查询用户")
    ResultData detail(Long id) {
        log.trace("trace日志");
        log.debug("debug日志");
        log.info("info日志");
        log.warn("warn日志");
        log.error("error日志");
        return ResultData.success(userMapper.selectById(id));
    }
    /**
     * 查询列表
     *
     * @return
     */
    @GetMapping("/listAllUser")
    @ApiOperation("查询用户列表")
    PageObject listAllUser() {
        Page<User> userPage = new Page<User>(1 , 2 , true);
        return new PageObject(userMapper.selectPage(userPage , null));
    }
    /**
     * 修改
     *
     * @return
     */
    @PutMapping("/updateById")
    @ApiOperation("修改用户")
    ResultData updateById(@Validated @RequestBody User user) {
        return ResultData.success(userMapper.updateById(user));
    }
    /**
     * 新增
     *
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("新增用户")
    ResultData add(@Validated @RequestBody User user) {
        userMapper.insert(user);
        return ResultData.success(user.getId());
    }
    /**
     * 删除
     *
     * @return
     */
    @DeleteMapping("/removeById")
    @ApiOperation("删除用户")
    ResultData removeById(Long id) {
        return ResultData.success(userMapper.deleteById(id));
    }
}
